from typing import Optional
from datetime import datetime
from pydantic import BaseModel, validator


class SysConfigBase(BaseModel):
    """系统配置基础模式"""
    code: Optional[str] = None
    value: Optional[str] = None
    remarks: Optional[str] = None


class SysConfigCreate(SysConfigBase):
    """系统配置创建模式"""
    code: str
    value: str

    @validator('code')
    def code_not_empty(cls, v):
        if not v or not v.strip():
            raise ValueError('配置代码不能为空')
        return v.strip()


class SysConfigUpdate(SysConfigBase):
    """系统配置更新模式"""
    version: int  # 用于乐观锁控制


class SysConfigInDBBase(SysConfigBase):
    """数据库中的系统配置模式基类"""
    id: int
    version: int
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None
    create_by: Optional[str] = None
    update_by: Optional[str] = None

    class Config:
        orm_mode = True


class SysConfig(SysConfigInDBBase):
    """返回给客户端的系统配置模式"""
    pass


class SysConfigInDB(SysConfigInDBBase):
    """数据库中的系统配置模式，完整信息"""
    pass